package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    GraphInfoService graphInfoService;

    @Override
    public IPage<RoomItemVo> getPageItemVo(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        //先查房间的集合和它对应的公寓集合
        IPage<RoomItemVo> pageItemVo = roomInfoMapper.getPageItemVo(page, queryVo);


        //在通过房间集合查房间标签

        List<RoomItemVo> records = pageItemVo.getRecords();

        records.stream().forEach(f -> {
            List<LabelInfo> labelInfoList = new ArrayList<>();
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, f.getId());
            List<RoomLabel> roomLabelList = roomLabelService.list(roomLabelLambdaQueryWrapper);
            roomLabelList.forEach(r -> {
                Long labelId = r.getLabelId();
                LabelInfo byId = labelInfoService.getById(labelId);
                labelInfoList.add(byId);
            });


            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).
                    eq(GraphInfo::getItemId, f.getId());

            List<GraphInfo> list = graphInfoService.list(graphInfoLambdaQueryWrapper);
            List<GraphVo> graphVoList = new ArrayList<>();
            list.forEach(l -> {
                GraphVo graphVo = new GraphVo();
                BeanUtils.copyProperties(l, graphVo);
                graphVoList.add(graphVo);
            });

            f.setGraphVoList(graphVoList);
            f.setLabelInfoList(labelInfoList);
        });

        return pageItemVo;


    }
}




